package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 玩乐住, 美食街,石子街 公用的请求数据了类-列表 异步请求
 */
public class GetUserDetail extends BaseCommand {


    private int page = DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码
    private String TAG = "GetUserDetail";
    private String userid;

    public GetUserDetail(String userid) {
        this.userid = userid;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_USER_DETAIL;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("userid", userid);
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.postByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetUserDetail.yes()");

        try {
            Log.d(TAG, result.object.toString());

            JSONObject jsonObject = new JSONObject(result.object.toString());
            String code = jsonObject.getString("code");
            String msg = jsonObject.getString("msg");
            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
            ArrayList<UserBean> targetList = DataCenter.userBeans;
            targetList.clear();
            UserBean userBean = new UserBean();
            userBean.setUserid(jsonObject1.getString("userid"));
            userBean.setUsername(jsonObject1.getString("username"));
            userBean.setTruename(jsonObject1.getString("truename"));
            userBean.setAddress(jsonObject1.getString("address"));
            userBean.setQq(jsonObject1.getString("qq"));
            userBean.setMobile(jsonObject1.getString("mobile" + ""));
            targetList.add(userBean);
            DataCenter.code = code.toString();
            DataCenter.msg = msg.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_USER_DETAIL_SUCCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetUserDetail.no()");

        DataCenter.userLogin.clear();
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_USER_DETAIL_FAILED);

    }
}
